<?php 
    
     /**
     * @author ChessMax
     * @copyright 2010
     */
     
     class Auth #extends Connect
     {    
        public $flashVars;
        public $success = true;
        public $error;
        public $message;
        private $DB;
        #public $locations;        
        
        
        function __construct($flashVars)
        {
            #parent::__construct($flashvars);
            #return Auther::authorized($flashVars);
            #$this->flashVars = $flashVars;
            if(auther::authorized($flashVars)){
                $this->flashVars = $flashVars;
            }else{
                $this->success = false;
                $this->error = "Auth Error";
            }
            #$this->DB = new DBConnect();
        }            
        
        public function _auth()
        {
            #return Auther::authorized($this->flashVars);
            #$DB = new DBConnect(SERVER_DB, USER_DB, PASS_DB, DB, PREFIX_TABLE);
            if($this->success != true) return;
            #$this->locations = new Help();
            if(Auther::checkAuth($this->flashVars['viewer_id']) === true){
                $this->success = true;                
                if( isset($_SESSION['user']['lasttime']) ) $_SESSION['user']['lasttime'] = time(); #$arr['time'];
                #return;
            }
            $this->flashVars = DBConnect::checkData($this->flashVars); 
            
            $sql = "SELECT * FROM `".PREFIX_TABLE."users` WHERE 
                `kontaktid` = '".$this->flashVars['viewer_id']."' LIMIT 1";
            
            if(!$result = DBConnect::query($sql)){
                $this->success = false;
                $this->error = mysql_error();
                return false;
            }
            $arr = mysql_fetch_array($result);
            #$arr = DBConnect::getOne($result);
            #$arr = $this->DB->fetch_assoc();
            #return $arr; 
            if(!empty($arr['id'])) {
                $this->success = true;
                Auther::sessionStart($this->flashVars['viewer_id'], $this->flashVars);
                $sql = "UPDATE `".PREFIX_TABLE."users` set `lasttime` = '".$arr['time']."', `time` = '".time()."' 
                                WHERE `kontaktid` = '".$this->flashVars['viewer_id']."' ";
                
                if(!$result = DBConnect::query($sql)){
                    $this->success = false;
                    $this->error = mysql_error();
                    return false;
                }
                $stat = new Statistic();
                $stat->newGame($this->flashVars['viewer_id']); 
                #$this->locations->setAllOffString($arr['location']);
                
                /*$arr['lasttime'] = $arr['time'];
                $arr['time'] = time();*/
                return ;
            }
            else{
                $this->success = false;
                $this->error = "user no found";
            }
            
            
            //return $this->update();
        }

        
        
        /**
         * 
         * */
        public function newUSer($data)
        {
            if($this->success != true) return;                        
            $data = DBConnect::checkData($data);
            
            $sql = "SELECT * FROM `".PREFIX_TABLE."users` WHERE 
                `kontaktid` = '".$this->flashVars['viewer_id']."' LIMIT 1";
            #return $sql;
            if(!$result = DBConnect::query($sql)){
                $this->success = false;
                $this->error = mysql_error();
                return false;
            }
            $arr = mysql_fetch_array($result); 
            if(!empty($arr['id'])) {
                $this->success = false;
                $this->error = "Уже есть";
                return;
            }
            
            $year = @getdate( @strtotime($data['bdate']) );
            #$this->locations = new Help();            
            _echo($year);
            $sql = "INSERT INTO `" .PREFIX_TABLE. "users` (
                            `kontaktid`, `sex`, 
                            `skin`, `level`, 
                            `pet`, `levelpet`, 
                            `lasttime`, `yearCreate`, `timeSetLevel`, `time`)
                    VALUES ('".$this->flashVars['viewer_id']."',
                            '".$data['sex']."',
                            '".$data['skin']."', 
                            '1',
                            '1',
                            '1',
                            '".time()."',
                            '".@$year['year']."',
                            '".time()."',
                            '".time()."')
                    ON DUPLICATE KEY UPDATE `kontaktid` = VALUES(`kontaktid`)";
            #return $sql;
            #_echo($sql);
            if(!$result = DBConnect::query($sql)){
                $this->success = false;
                $this->error = mysql_error();
                return false;
            }        
            $this->success = true;
            $this->message = "new user";
            Auther::sessionStart($this->flashVars['viewer_id'], $this->flashVars);
            return true;
        }
     } 
?>